package com.atguigu.team.service;

import com.atguigu.team.domain.Architect;
import com.atguigu.team.domain.Designer;
import com.atguigu.team.domain.Employee;
import com.atguigu.team.domain.Equipment;
import com.atguigu.team.domain.NoteBook;
import com.atguigu.team.domain.PC;
import com.atguigu.team.domain.Printer;
import com.atguigu.team.domain.Programmer;
/**
 * 
* @Description 负责将Data中的数据封装到Employee[]数组中，同时提供相关操作Employee[]的方法。
* @author yihu Email:1057202547@qq.com
* @version
* @date 2020年11月19日上午11:13:47
*
 */
public class NameListService {
	
	private Employee[] employees;
	
	public NameListService() {
		initData();
	}
	/**
	 *  给employees 初始化
	 * 	根据项目提供的Data类构建相应大小的employees数组
		再根据Data类中的数据构建不同的对象，包括Employee、Programmer、Designer和Architect对象，以及相关联的Equipment子类的对象
		将对象存于数组中
		Data类位于com.atguigu.team.service包中
	 */
	private void initData() {
		employees = new Employee[Data.EMPLOYEES.length];
		for (int i = 0; i < employees.length; i++) {
			int type = Integer.parseInt(Data.EMPLOYEES[i][0]);
			//获取id, name, age, salary 四个基本信息
			int id = Integer.parseInt(Data.EMPLOYEES[i][1]);
			String name = Data.EMPLOYEES[i][2];
			int age = Integer.parseInt(Data.EMPLOYEES[i][3]);
			double salary = Double.parseDouble(Data.EMPLOYEES[i][4]);
			Equipment equipment;
			double bonus;
			switch (type) {
			case Data.EMPLOYEE:
				//员工
				employees[i] = new Employee(id, name, age, salary);
				break;
			case Data.PROGRAMMER:
				//程序员
				equipment = creteEquipment(i);
				employees[i] = new Programmer(id, name, age, salary, equipment);
				break;
			case Data.DESIGNER:
				//设计师
				equipment = creteEquipment(i);
				bonus =Double.parseDouble(Data.EMPLOYEES[i][5]);
				employees[i] = new Designer(id, name, age, salary, equipment, bonus);
				break;
			case Data.ARCHITECT:
				//架构师
				equipment = creteEquipment(i);
				bonus =Double.parseDouble(Data.EMPLOYEES[i][5]);
				int stock = Integer.parseInt(Data.EMPLOYEES[i][6]);
				employees[i] = new Architect(id, name, age, salary, equipment, bonus, stock);
				break;
			default:
				break;
			}
		}
	}
	
	/**
	 * 
	* @Description 获取指定index位置上的设备
	* @author yihu Email:1057202547@qq.com
	* @date 2020年11月19日上午11:36:00
	* @param i
	* @return
	 */
	private Equipment creteEquipment(int i) {
		Equipment equipment = null;
		int equipmenttype = Integer.parseInt(Data.EQIPMENTS[i][0]);
		String model = Data.EQIPMENTS[i][1];
		switch (equipmenttype) {
			case Data.PC: //21
				equipment = new PC(model, Data.EQIPMENTS[i][2]);
				break;
			case Data.NOTEBOOK: //22
				equipment = new NoteBook(model, Double.parseDouble(Data.EQIPMENTS[i][2]));
				break;
			case Data.PRINTER: //23
				equipment = new Printer(model, Data.EQIPMENTS[i][2]);
				break;
			default:
				break;
		}
		return equipment;
	}
	
	/**
	 * 获取当前所有员工
	* @Description 
	* @author yihu Email:1057202547@qq.com
	* @date 2020年11月20日上午11:25:36
	* @return
	 */
	
	public Employee[] getAllEmployees() {
		return employees;
	}
	
	/**
	 * 
	* @Description 获取指定id员工的对象
	* @author yihu Email:1057202547@qq.com
	* @date 2020年11月20日上午11:28:07
	* @param id
	* @return
	 */
	public Employee getEmployee(int id) throws TeamException{
		
		for (int i = 0; i < employees.length; i++) {
			if(employees[i].getId() == id) {
				return employees[i];
			}
		}
	
		throw new TeamException("找不到指定的员工");
	}
	
	
	
}
